# 定义棋盘大小
BOARD_SIZE = 9

# 初始化棋盘
def init_board():
    return [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

# 打印棋盘
def print_board(board):
    print('  ', end='')
    for i in range(BOARD_SIZE):
        print(i, end=' ')
    print()
    for i in range(BOARD_SIZE):
        print(i, end=' ')
        for j in range(BOARD_SIZE):
            print(board[i][j], end=' ')
        print()

# 判断落子位置是否合法
def is_valid_move(board, x, y, player):
    if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE:
        return False
    if board[x][y] != ' ':
        return False
    return True

# 落子
def make_move(board, x, y, player):
    board[x][y] = player
    return board

# 判断是否平局（棋盘填满）
def is_full(board):
    for row in board:
        if ' ' in row:
            return False
    return True

# 主游戏循环
def play_game():
    board = init_board()
    current_player = 'X'  # 'X' 先手
    while True:
        print_board(board)
        print(f"当前玩家: {current_player}")
        try:
            x, y = map(int, input("请输入落子的坐标 (x y): ").split())
            if is_valid_move(board, x, y, current_player):
                board = make_move(board, x, y, current_player)
                if is_full(board):
                    print_board(board)
                    print("平局！棋盘已满。")
                    break
                # 切换玩家
                current_player = 'O' if current_player == 'X' else 'X'
            else:
                print("无效的落子位置，请重新输入。")
        except ValueError:
            print("输入无效，请输入两个整数。")

if __name__ == "__main__":
    play_game()